<template>
  <el-container>
    <el-aside width="200px" class="aside-container">
      <!-- 项目名称和Logo区域 -->
      <div class="logo-container">
        <img src="../assets/logo.png"
             alt="用户管理系统logo"
             class="logo-img">
        <span class="system-name">用户管理系统</span>
      </div>
      <!-- 菜单区域 -->
      <el-menu
          default-active="0"
          class="el-menu-vertical-demo"
          background-color="#304156"
          text-color="#fff"
          active-text-color="#ffd04b">
        <el-menu-item index="0">
          <i class="el-icon-s-home"></i>
          <span slot="title">
              <router-link to="/admin/" tag="span">首页</router-link>
          </span>
        </el-menu-item>
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-s-tools"></i>
            <span>系统管理</span>
          </template>
          <el-menu-item index="1-1">
            <router-link to="/admin/user" tag="span">用户管理</router-link>
          </el-menu-item>
          <el-menu-item index="1-2">角色管理</el-menu-item>
          <el-menu-item index="1-3">日志管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header class="header-container">
        <!-- 头部右侧用户区域 -->
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info">
              <img :src="user.avatar" alt="用户头像" class="user-avatar">
              <span class="user-name">{{ user.username }}</span>
              <i class="el-icon-arrow-down user-arrow"></i>
            </div>
            <el-dropdown-menu slot="dropdown" class="user-dropdown">
              <el-dropdown-item command="center">
                <i class="el-icon-user"></i> 个人中心
              </el-dropdown-item>
              <el-dropdown-item command="logout">
                <i class="el-icon-switch-button"></i> 退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import {mapState} from 'vuex'
import {logout} from "@/api/user";
export default {
  name: "AdminView",
  data() {
    return {
    }
  },
  computed:mapState(['user']),
  methods: {
    // 处理下拉菜单命令
    handleCommand(command) {
      if (command === 'center') {
        // 跳转到个人中心逻辑
        console.log('进入个人中心')
      } else if (command === 'logout') {
        // 退出登录逻辑
        console.log('退出登录')
        this.$message.info('退出登录');
        logout().then(res =>{
          // 退出之前,清空vuex
          this.$store.commit('saveUserInfo',null)
          // 到登录页面
          this.$router.push("/")
        })


      }
    }
  }
}
</script>

<style scoped>
/* 头部样式 */
.header-container {
  background-color: #F4F4F5;
  color: #333;
  line-height: 60px;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: flex-end;
}

.header-right {
  display: flex;
  align-items: center;
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 10px;
  height: 60px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 8px;
  object-fit: cover;
}

.user-name {
  font-size: 14px;
  color: #333;
  margin-right: 5px;
}

.user-arrow {
  font-size: 12px;
  color: #666;
  transition: transform 0.3s;
}

.el-dropdown.open .user-arrow {
  transform: rotate(180deg);
}

/* 下拉菜单样式 */
.user-dropdown {
  width: 160px;
}

.user-dropdown .el-dropdown-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
}

.user-dropdown .el-dropdown-item i {
  margin-right: 8px;
  width: 16px;
  text-align: center;
}

/* 侧边栏样式 */
.aside-container {
  background-color: #304156;
  color: #8CC2D9;
  height: 710px;
  display: flex;
  flex-direction: column;
}

.logo-container {
  display: flex;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  border-bottom: 1px solid #475669;
}

.logo-img {
  width: 30px;
  height: 30px;
  margin-right: 10px;
}

.system-name {
  color: #fff;
  font-size: 16px;
  font-weight: 500;
}

.el-menu-vertical-demo {
  border-right: none;
  flex: 1;
  overflow-y: auto;
}

/* 主内容区样式 */
.el-main {
  background-color: #f3f3f4;
  color: #333;
  //text-align: center;
  //line-height: 160px;
  //padding: 20px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
